var bgW = $(window).width();
bgW = 640;
var bgH = $(window).height();
var state = $.cookie("state");
var openid = $.cookie("openid");
var id = getQueryString("id");

//var shuohua,xingming;
var username = "",headurl="";
//var openid = "";
$(function() {
	console.log(openid);
	if(openid=="" || openid==null)
	{
		location.href = encodeURI("fabu.html");
	}
	try{
		var suiji =parseInt(Math.random()*33);
		console.log(suiji);
		$("#wenan").attr("src","image/a"+suiji+".png");
		$.post("shuju.php?jieshou=checkopenid",{openid:openid},function(text){
			console.log(text);
			if(text=="0")
			{
				var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4156160c4f1640e1&redirect_uri=http://jkera.cn/xiadou/Userinfo.php?id=0&response_type=code&scope=snsapi_userinfo&state=fabu#wechat_redirect";
	            location.href = encodeURI(url);
				return;
			}
			var info = JSON.parse(text)[0];
			username = info.username;
			headurl = info.headurl;
			$("#touxiang").attr("src",openid+".png");
//			$("#touxiang").attr("src","image/play.png");
			try {
				//			gettupianlist();
				var image1 = "http://www.jkera.cn/xiadou/image/share.png";
				var url1 = "http://www.jkera.cn/xiadou/fabu.html";
				fenxiang("史上最强精神分析，洞察你的内心！", username+"人前搜索四大名著，人后搜索却是……这么丧心病狂！", "史上最强精神分析，洞察你的内心！", url1, image1, openid);
			} catch(err) {
				//alert(err);
			}
		})
	}catch(e){
		//TODO handle the exception
//		alert(e.message);
	}
	
//	try {
//		//			gettupianlist();
//		var image1 = "http://www.jkera.cn/xiadou/image/share.png";
//		var url1 = "http://www.jkera.cn/xiadou/fabu.html";
//		fenxiang(username+"人前搜索四大名著，人后搜索却是……这么丧心病狂！", username+"人前搜索四大名著，人后搜索却是……这么丧心病狂！", "史上最强精神分析，洞察你的内心！", url1, image1, openid);
//	} catch(err) {
//		//alert(err);
//	}

	if(openid == "" || openid == null) {
		//			openid = "oONuCv6hrgT-jNbqJRL5fFHf7IfQ";
		//			location.href = encodeURI("fabu.html");
	}
	$("#gameView,#bg,#lastimg").attr({
		"width": bgW,
		"height": bgH
	});


	// Converts canvas to an image
	var convertCanvasToImage = function() {
		var image = document.getElementById("lastimg");
		setTimeout(function() {
			var imgsrc = document.getElementById("gameView").toDataURL("image/png");
			image.src = imgsrc;
			$("#lastimg").css("display","block");
		}, 1000)

	}

	var music = false;
	var yinyue = function() {
		if(music == false) {
			$("#tubiao").attr("src","image/play.png");
			music = true;
			bgmp3.play();
		} else {
			$("#tubiao").attr("src","image/stop.png")
			music = false;
			bgmp3.pause();
		}
	}
	$("#tubiao").click(function() {
		yinyue();

	})

})